/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2025-06-09     Administrator       the first version
 */
#include <rtthread.h>
#include <rtdevice.h>
#include <board.h>
#include <bsp_key2.h>
//#include <bsp.h>


// 按键检测线程控制块
static rt_thread_t key_thread = RT_NULL;

// 按键检测线程入口函数
static void key_thread_entry(void *param) {
    rt_tick_t current_tick;
    rt_base_t current_state;

    while (1) {
        current_tick = rt_tick_get();

        for (int i = 0; i < KEY_COUNT; i++) {
            struct key_status *key = &keys[i];
            current_state = rt_pin_read(key->pin);

            // 状态0: 按键释放
            if (key->state == 0) {
                if (current_state == PIN_LOW) {  // 检测到按下
                    key->state = 1;  // 进入消抖状态
                    key->press_time = current_tick;
                }
            }
            // 状态1: 按下消抖
            else if (key->state == 1) {
                if (current_tick - key->press_time > rt_tick_from_millisecond(DEBOUNCE_TIME)) {
                    if (rt_pin_read(key->pin) == PIN_LOW) {  // 确认按下
                        key->state = 2;  // 进入按下状态
                    } else {
                        key->state = 0;  // 抖动，返回释放状态
                    }
                }
            }
            // 状态2: 按键按下
            else if (key->state == 2) {
                // 长按检测
                if (current_tick - key->press_time > rt_tick_from_millisecond(LONG_PRESS_TIME)) {
                    // 触发长按事件
                    if (key_event_handler) {
                        key_event_handler(i, 2);  // 2=长按
                    }
                    key->state = 3;  // 进入长按状态
                }

                // 检测释放
                if (rt_pin_read(key->pin) == PIN_HIGH) {
                    key->state = 3;  // 进入释放消抖状态
                    key->release_time = current_tick;
                }
            }
            // 状态3: 释放消抖
            else if (key->state == 3) {
                if (current_tick - key->release_time > rt_tick_from_millisecond(DEBOUNCE_TIME)) {
                    if (rt_pin_read(key->pin) == PIN_HIGH) {  // 确认释放
                        // 检查双击
                        if (current_tick - key->release_time < rt_tick_from_millisecond(DOUBLE_CLICK_TIME)) {
                            // 触发双击事件
                            if (key_event_handler) {
                                key_event_handler(i, 1);  // 1=双击
                            }
                        } else {
                            // 触发单击事件
                            if (key_event_handler) {
                                key_event_handler(i, 0);  // 0=单击
                            }
                        }
                        key->state = 0;  // 返回释放状态
                    } else {
                        key->state = 2;  // 抖动，返回按下状态
                    }
                }
            }
        }

        rt_thread_mdelay(10);  // 10ms检测周期
    }
}

// 初始化按键驱动
void key_driver_init(key_event_cb callback) {
    // 设置按键事件回调
    key_event_handler = callback;

    // 初始化所有按键引脚
    for (int i = 0; i < KEY_COUNT; i++) {
        rt_pin_mode(keys[i].pin, PIN_MODE_INPUT_PULLUP);
    }

    // 创建按键检测线程
    key_thread = rt_thread_create("key_detect",
                                 key_thread_entry,
                                 RT_NULL,
                                 512,  // 减小栈大小
                                 RT_THREAD_PRIORITY_MAX / 2,
                                 20);
    if (key_thread) {
        rt_thread_startup(key_thread);
        rt_kprintf("Simple Key Driver Initialized\n");
    } else {
        rt_kprintf("Key thread create failed!\n");
    }
}

// 示例使用
void key_event_handler_example(rt_uint8_t key_id, rt_uint8_t event_type) {
    const char *event_name[] = {"Click", "Double Click", "Long Press"};

    if (key_id < KEY_COUNT) {
        rt_kprintf("[%s] %s Detected\n", keys[key_id].name, event_name[event_type]);

        // 这里添加你的按键处理逻辑
        switch (key_id) {
        case 0: // KEY1
            if (event_type == 0) { // 单击
                rt_kprintf("Key1 Click!\n");
                led_control(LED_G_PIN, 1);
                // 处理KEY1单击
            }
            else if (event_type == 1) {
                rt_kprintf("Key1 Double Click!\n");
                led_control(LED_G_PIN, 1);
            }
            else if (event_type == 2) {
                rt_kprintf("Key1 Long Click!\n");
                led_control(LED_G_PIN, 0);
            }
            break;
        case 1: // KEY2
            if (event_type == 0) { // 双击
                rt_kprintf("Key2 Double Click!\n");
                led_control(LED_B_PIN, 1);
                // 处理KEY2双击
            }
            else if (event_type == 1) {
                rt_kprintf("Key2 Double Click!\n");
                led_control(LED_B_PIN, 1);
            }
            else if (event_type == 2) {
                rt_kprintf("Key2 LONG Click!\n");
                led_control(LED_B_PIN, 1);
            }
            break;
        case 2: // KEY3
            if (event_type == 0) { // 双击
                rt_kprintf("Key3 Double Click!\n");
                rgb_led_control(1, 1, 1);
                // 处理KEY2双击
            }
            else if (event_type == 1) {
                rt_kprintf("Key3 Double Click!\n");
                rgb_led_control(1, 1, 1);
            }
            else if (event_type == 2) {
                rt_kprintf("Key3 LONG Click!\n");
                rgb_led_control(0, 0, 0);
                // 处理KEY3长按
            }
            break;
        }
    }
}

/*
// 主函数
int main(void) {
    // 初始化按键驱动，传入事件处理函数
    key_driver_init(key_event_handler_example);

    return 0;
}*/
